package com.da.lon.wang.xlg.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.da.lon.wang.xlg.R;
import com.umeng.analytics.MobclickAgent;

public class InvoiceActivity extends BaseActivity implements OnClickListener {

    protected String invoice = null;
    protected String invoiceType = null;
    private ImageButton backButton;
    private RadioGroup invoiceGroup;
    private RadioButton noRadioButton, paperRadioButton;
    private Button okButton;
    private EditText invoiceEditText;
    private int resultCode = 3;
    private String invoiceText = null;
    private RadioGroup.OnCheckedChangeListener mChangeRadio = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            if (checkedId == noRadioButton.getId()) {
                invoice = getString(R.string.no_need_for_invoice);
                invoiceType = "0";
            } else if (checkedId == paperRadioButton.getId()) {
                invoice = getString(R.string.paper_invoice);
                invoiceType = "1";
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ouka_order_invoice);

        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void initView() {
        backButton = (ImageButton) findViewById(R.id.ouka_invoice_left);
        backButton.setOnClickListener(this);
        invoiceGroup = (RadioGroup) findViewById(R.id.ouka_invoice_rg);
        invoiceGroup.setOnCheckedChangeListener(mChangeRadio);
        noRadioButton = (RadioButton) findViewById(R.id.no_invoice);
        paperRadioButton = (RadioButton) findViewById(R.id.paper_invoice);
        okButton = (Button) findViewById(R.id.invoice_ok);
        okButton.setOnClickListener(this);
        invoiceEditText = (EditText) findViewById(R.id.ouka_invoice_et);
        invoice = getIntent().getStringExtra("invoice");
        invoiceText = getIntent().getStringExtra("invoiceText");
        invoiceType = getIntent().getStringExtra("invoiceType");
        if (invoiceType != null && invoiceType.equals("0")) {
            noRadioButton.setChecked(true);
            paperRadioButton.setChecked(false);
        } else if (invoiceType != null && invoiceType.equals("1")) {
            noRadioButton.setChecked(false);
            paperRadioButton.setChecked(true);
        } else {
            noRadioButton.setChecked(false);
            paperRadioButton.setChecked(false);
        }
        if (invoiceText != null) {
            invoiceEditText.setText(invoiceText);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ouka_invoice_left:
                finish();
                break;

            case R.id.invoice_ok:
                Intent intent = new Intent();
                intent.putExtra("invoice", invoice);
                intent.putExtra("invoiceText", invoiceEditText.getText().toString());
                intent.putExtra("invoiceType", invoiceType);
                setResult(resultCode, intent);
                finish();
                break;

            default:
                break;
        }
    }
}
